#!/bin/sh

. /lib/functions/uci-defaults.sh
. /lib/functions/system.sh

rockchip_setup_interfaces()
{
	local board="$1"

	case "$board" in
	friendlyarm,nanopi-r2s|\
	friendlyarm,nanopi-r4s)
		ucidef_set_interfaces_lan_wan 'eth1' 'eth0'
		;;
	*)
		ucidef_set_interface_lan 'eth0'
		;;
	esac
}

nanopi_r2s_generate_mac()
{
	local sd_hash=$(sha256sum /sys/devices/platform/ff500000.dwmmc/mmc_host/mmc0/mmc0:*/cid)
	local mac_base=$(macaddr_canonicalize "$(echo "${sd_hash}" | dd bs=1 count=12 2>/dev/null)")
	echo "$(macaddr_unsetbit_mc "$(macaddr_setbit_la "${mac_base}")")"
}

rockchip_setup_macs()
{
	local board="$1"
	local lan_mac=""
	local wan_mac=""
	local label_mac=""

	case "$board" in
	friendlyarm,nanopi-r2s)
		wan_mac=$(nanopi_r2s_generate_mac)
		lan_mac=$(macaddr_add "$wan_mac" +1)
		;;
	friendlyarm,nanopi-r4s)
		wan_mac=$(cat /sys/class/net/eth0/address)
		lan_mac=$(macaddr_add "$wan_mac" +1)
		;;
	esac

	[ -n "$lan_mac" ] && ucidef_set_interface_macaddr "lan" $lan_mac
	[ -n "$wan_mac" ] && ucidef_set_interface_macaddr "wan" $wan_mac
	[ -n "$label_mac" ] && ucidef_set_label_macaddr $label_mac
}

board_config_update
board=$(board_name)
rockchip_setup_interfaces $board
rockchip_setup_macs $board
board_config_flush

exit 0
